package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Label;
import java.awt.RenderingHints;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import control.MouseListener;
import javax.swing.JButton;

import model.GraphAPI;
import model.Measurement;
import model.Variables;

public class WeatherPanel extends JPanel {
	MainF mainF;
	Variables variables;
	Image bgImage = null;
	double[][] data;
	boolean drawPicture;
	boolean drawWeatherStats;
	GraphAPI weatherGraph;
	/**
	 * Create the panel.
	 */
	public WeatherPanel(MainF mainF) {
		this.mainF = mainF;
		this.variables = mainF.getModel().getVariables();
		setBackground(new Color(45,138,239));
		setPreferredSize(new Dimension(482, 452));
		setLayout(null);
		setName("WeatherPanel");
		
		drawPicture = true;
		drawWeatherStats = false;
		
		
		JButton btnNewButton = new JButton("New button");
		btnNewButton.setBounds(10, 40, 89, 23);
		add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("New button");
		btnNewButton_1.setBounds(157, 167, 89, 23);
		add(btnNewButton_1);
		
		
		
		
//		Random r = new Random();
//		data =new double[40][1];
//		for(int j = 0;j<1;j++){
//		for(int i= 0;i<40;i++){
//			data[i][j]=i+r.nextInt(100);
//		}
//		}
		
	
		
		MouseListener mL = new MouseListener(mainF);
		addMouseListener(mL);
	}
	
	
	public void addAll(){
		
	}
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		if(drawPicture==true){
		try {
			ImageIcon ii = (ImageIcon) variables.getImgWeatherIcon();
			bgImage = ii.getImage();
		} catch (Exception ex) {
			// Error message if file dosn't exist
			String message = "File load failed: " + ex.getMessage();
			g.drawString(message, 40, 40);
		}
		// Draws picture
			g2.drawImage(bgImage, 450, 0, 32, 32, null);
		}
		
		if(drawWeatherStats){
		if(mainF.getModel().getMeasurements().size()>0){
			Font font = variables.getFontTileHeader();
			font = font.deriveFont(20f);
			g2.setFont(font);
			g2.drawString("Current Temp:", 30, 50);
			g2.drawString("Current Light:", 30, 70);
			
			Measurement measurement = mainF.getModel().getMeasurements().get(mainF.getModel().getMeasurements().size()-1);
			FontMetrics metrics = g2.getFontMetrics(g2.getFont());
			int tempSize = metrics.stringWidth(""+measurement.getTemp());
			int lightSize = metrics.stringWidth(""+measurement.getLight());
			
			g2.drawString("" + measurement.getTemp(), 350 - tempSize, 50);
			g2.drawString("" + measurement.getLight(), 350 - lightSize, 70);
			ArrayList<Measurement> measurements = mainF.getModel().getMeasurements();
			data = new double[measurements.size()][2];
			for(Measurement tempMeasurement:measurements){
				data[measurements.indexOf(tempMeasurement)][1] = tempMeasurement.getTemp();
				data[measurements.indexOf(tempMeasurement)][0] = tempMeasurement.getLight();
				
			}
			
			weatherGraph = new GraphAPI();
			weatherGraph.getColors().add(0, new Color(255,0,0));
			weatherGraph.getColors().add(1, new Color(0,0,255));
			weatherGraph.getGraphType().add(0, true);
			weatherGraph.getNames().add(0, "Light");
			weatherGraph.getNames().add(1, "Temp");
			
			weatherGraph.paintDataGraph(g2, data, 45, 200, 375, 175);
			//GraphAPI.paintLinesMouse(g2, data, 45, 200, 375, 175 ,MX,lockedRightMY,1);
		}
		}
		
		
	}


	public boolean isDrawPicture() {
		return drawPicture;
	}


	public void setDrawPicture(boolean drawPicture) {
		this.drawPicture = drawPicture;
	}


	public boolean isDrawWeatherStats() {
		return drawWeatherStats;
	}


	public void setDrawWeatherStats(boolean drawWeatherStats) {
		this.drawWeatherStats = drawWeatherStats;
	}
}
